import { app, callCloud, lib } from "./common/index";

export async function upload(file, prefix){
  const cloudPath = prefix || file.name
  return app.uploadFile({
    // 云存储的路径
    cloudPath: cloudPath,
    // 需要上传的文件，File 类型
    filePath: file
  })
}

export async function deleteFile(files=[]) {
  files = [].concat(files)
  return app.deleteFile({
    fileList: files
  })
}

export async function downloadFile(fileId) {
  return app.downloadFile({
    fileID: fileId
  })
}

export async function templateUrl(files=[]) {
  files = [].concat(files)
  return app.getTempFileURL({
    fileList: files
  })
}

export async function getStoreageFiles(param={}, cb){
  const baseUrl = (this && this.baseUrl) || 'storage/command'
  return new Promise((resolve, rej)=>{
    callCloud(baseUrl + '/getFiles/', param).then(res=>{
      const baseUrl = res.result.baseUrl
      const result = res.result.data
      result.forEach(item=>{
        item.fileId = baseUrl + '/' + item.Key
      })
      if (lib.isFunction(cb)) cb(result)
      resolve(result)
    })
  })
}